﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace ApplicationShell
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private SalesManagerEntities _sme = new SalesManagerEntities();
        private string _userName = string.Empty;
        private CollectionViewSource _clientViewSource;

        public MainWindow()
        {
            InitializeComponent();

            _clientViewSource = (CollectionViewSource)FindResource("ClientListViewSource");
            _sme.ContextOptions.LazyLoadingEnabled = true;

            _clientViewSource.Source = from c in _sme.tblOwner select c;

            foreach (tblApplication application in _sme.spUserApplications(_sme.CurrentUserID()))
            {
                ApplicationLauncher app = new ApplicationLauncher(application);

                app.OnLaunchApplication += new EventHandler<ApplicationLauncher.LaunghApplicationEventArgs>(app_OnLaunchApplication);

                panel1.Children.Add(app);
            }
            
        }

        void app_OnLaunchApplication(object sender, ApplicationLauncher.LaunghApplicationEventArgs e)
        {
            string arguments = e.Arguments;
            if (arguments != null && _clientViewSource.View.CurrentItem != null)
                arguments = arguments.Replace("@OwnerID", ((tblOwner)_clientViewSource.View.CurrentItem).OwnerID.ToString());
            System.Diagnostics.Process.Start(e.Path, arguments);
        }
    }
}
